package com.engine4cloud.tdf.supervision.convert;

import com.engine4cloud.tdf.common.core.constant.enums.CorElectronicFenceRuleTypeEnum;
import com.engine4cloud.tdf.common.core.constant.enums.ElectronicFenceTypeEnum;
import com.engine4cloud.tdf.common.core.util.CodeUtils;
import com.engine4cloud.tdf.supervision.dto.JzdxElectronicFenceDTO;
import com.engine4cloud.tdf.supervision.dto.create.JzdxElectronicFenceCreateDTO;
import com.engine4cloud.tdf.supervision.dto.result.JzdxElectronicFenceResultDTO;
import com.engine4cloud.tdf.supervision.dto.result.JzdxResultDTO;
import com.engine4cloud.tdf.supervision.dto.update.JzdxElectronicFenceUpdateDTO;
import com.engine4cloud.tdf.supervision.entity.JzdxElectronicFenceEntity;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

import java.util.Objects;

@Mapper
public interface JzdxElectronicFenceConvert {
    JzdxElectronicFenceConvert INSTANCE = Mappers.getMapper(JzdxElectronicFenceConvert.class);

    JzdxElectronicFenceEntity createTo(JzdxElectronicFenceCreateDTO s);

    JzdxElectronicFenceEntity updateTo(JzdxElectronicFenceUpdateDTO s);

    JzdxElectronicFenceResultDTO convertTo(JzdxElectronicFenceEntity s);

    default JzdxElectronicFenceEntity create(JzdxElectronicFenceCreateDTO s, String fenceName, Long tenantId) {
        JzdxElectronicFenceEntity entity = createTo(s);
        entity.setTenantId(tenantId);
        entity.setWlmc(fenceName);
        entity.setLcbh(CodeUtils.entrustCode());
        if (Objects.equals(ElectronicFenceTypeEnum.AREA, s.getWllx())) {
            entity.setWlbh(s.getAreaFenList().get(s.getAreaFenList().size() - 1));
        }
        return entity;
    }

    default JzdxElectronicFenceEntity update(JzdxElectronicFenceUpdateDTO s, ElectronicFenceTypeEnum type, String fenceName) {
        JzdxElectronicFenceEntity entity = updateTo(s);
        entity.setWlmc(fenceName);
        if (Objects.equals(ElectronicFenceTypeEnum.AREA, type)) {
            entity.setWlbh(s.getAreaFenList().get(s.getAreaFenList().size() - 1));
        }
        return entity;
    }

    default JzdxElectronicFenceResultDTO convert(JzdxElectronicFenceEntity s) {
        JzdxElectronicFenceResultDTO result = convertTo(s);
        if (Objects.isNull(result)) {
            return null;
        }

        result.setXzgzmc(s.getXzgz().getDesc());
        result.setWllxmc(s.getWllx().getDesc());
        return result;
    }

    default JzdxElectronicFenceEntity createDefault(JzdxResultDTO dx, JzdxElectronicFenceDTO fence, Long tenantId) {
        if (Objects.isNull(dx) || Objects.isNull(fence)) {
            return null;
        }

        JzdxElectronicFenceEntity entity = new JzdxElectronicFenceEntity();
        entity.setRybh(dx.getRybh());
        entity.setLcbh(CodeUtils.entrustCode());
        entity.setWllx(ElectronicFenceTypeEnum.AREA);
        entity.setXzgz(CorElectronicFenceRuleTypeEnum.RESTRICTED_DEPARTURE);
        entity.setWlbh(fence.getCode());
        entity.setWlmc(fence.getName());
        entity.setTenantId(tenantId);

        return entity;
    }

}
